import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import type { userData } from "../api/type/userType";

const userSlice = createSlice({
    name:"user",
    initialState:{
        isLogin:false,
        user:{} as userData
    },
    reducers:{
        login(state,action:PayloadAction<userData>){
            return{
                ...state,
                isLogin:true,
                user:action.payload
            }
        },
        logout(state){
            return{
                ...state,
                isLogin:false,
                user:{} as userData
            }
        },
        updateUser(state,action:PayloadAction<userData>){
            return{
                ...state,
                isLogin:true,
                user:action.payload
            }
        }
    }
})

//actionCreater
export const {login,logout,updateUser} = userSlice.actions;

//reducer
export const userReducer = userSlice.reducer;